package cn.edu.njuit.springbootfilmreview.service.impl;

import cn.edu.njuit.springbootfilmreview.dto.ValidateCode;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Random;
import java.util.UUID;

/**
 * @Author guzy
 * @Description 验证码服务
 * @Date 2022/3/30 10:27
 **/

@Component
public class ValidateCodeService {

    public static HashMap<String, String> ValidateCodeStore = new HashMap<>();

    /**
     * 产生一个验证码
     */
    public ValidateCode createValidate() {
        //随机码
        String randomCode = UUID.randomUUID().toString();
        //2个随机数
        Random random = new Random();
        Integer number1 = random.nextInt(10);
        Integer number2 = random.nextInt(10);
        //算式
        String imageText = number1 + "+" + number2 + "=?";
        //答案
        String result = String.valueOf(number1 + number2);
        //存储到Store中,随机码作为键，算是和答案作为值，用逗号隔开
        ValidateCodeStore.put(randomCode, imageText + "," + result);

        ValidateCode validateCode = new ValidateCode(randomCode);
        return validateCode;
    }

    /**
     * 校验验证码是否正确
     */
    public boolean validate(String randomCode, String validateCode) {
        if (ValidateCodeStore.containsKey(randomCode)) {
            String validateString = ValidateCodeStore.get(randomCode);
            String answer = validateString.split(",")[1];

            //校验一次，无论对错立即失效
            ValidateCodeStore.remove(randomCode);
            return answer.equals(validateCode);
        }
        return false;
    }

    /**
     * 根据随机码获取验证码算是文本
     */
    public String getValidateText(String randomCode) {
        if (ValidateCodeStore.containsKey(randomCode)) {
            String validateString = ValidateCodeStore.get(randomCode);
            return validateString.split(",")[0];
        }
        return null;
    }


    /**
     * 产生  减法   一个验证码
     */
    public ValidateCode createSubValidate() {
        //随机码
        String randomCode = UUID.randomUUID().toString();
        //2个随机数
        Random random = new Random();
        Integer number1 = random.nextInt(10);
        Integer number2 = random.nextInt(10);


        String imageText;//算式
        String result;//答案

        if (number1 > number2) {
            imageText = number1 + "-" + number2 + "=?";
            result = String.valueOf(number1 - number2);
        } else {
            imageText = number2 + "-" + number1 + "=?";
            result = String.valueOf(number2 - number1);
        }
        //存储到Store中,随机码作为键，算是和答案作为值，用逗号隔开
        ValidateCodeStore.put(randomCode, imageText + "," + result);

        ValidateCode validateCode = new ValidateCode(randomCode);
        return validateCode;
    }


    /**
     * 产生  乘法  一个验证码
     */
    public ValidateCode createMulValidate() {
        //随机码
        String randomCode = UUID.randomUUID().toString();
        //2个随机数
        Random random = new Random();
        Integer number1 = random.nextInt(10);
        Integer number2 = random.nextInt(10);

        //算式
        String imageText = number1 + "*" + number2 + "=?";
        //答案
        String result = String.valueOf(number1 * number2);
        //存储到Store中,随机码作为键，算是和答案作为值，用逗号隔开
        ValidateCodeStore.put(randomCode, imageText + "," + result);

        ValidateCode validateCode = new ValidateCode(randomCode);
        return validateCode;
    }
}
